<?php

namespace seraph_pds;

// Exit if accessed directly
if( !defined( 'ABSPATH' ) )
	exit;

// #######################################################################

include( "common.php" );
include( "main_ext.php" );
include( "helper.php" );

// #######################################################################

Plugin::Init();

// #######################################################################

function OnInitAdminMode()
{
	add_action( 'add_meta_boxes',			'seraph_pds' . '\\' . '_add_post_meta_box' );

	add_action( 'admin_footer',				'seraph_pds' . '\\' . '_load_javascript' );
	add_action( 'admin_enqueue_scripts',	'seraph_pds' . '\\' . '_admin_style' );
	add_action( 'admin_menu',				'seraph_pds' . '\\' . '_OnAdminMenu_Show' );

	add_action( 'admin_action_seraph_pds_ApiCall',		'seraph_pds' . '\\' . '_OnAdminApiCall' );
}

function _OnAdminApiCall()
{
	_api_ProcessRequest( $_REQUEST[ 'func' ] );
}

// #######################################################################
// Post Edit

function _add_post_meta_box()
{
	$rmtCfg = PluginRmtCfg::Get();
	$sett = Plugin::SettGet();

	$urlHelp = Gen::GetArrField( $rmtCfg, 'Help.PostInplaceUpdater' );

	foreach( GetCompatiblePostsTypes() as $postType )
	{
		if( !Gen::GetArrField( $sett, array( 'docTypes', $postType, 'enable' ), true ) )
			continue;

		add_meta_box(
			'seraph_pds_add_post',
			__( '.DOCX Source', 'seraph_pds' ) . Ui::AdminHelpBtn( $urlHelp, Ui::AdminHelpBtnModeBlockHeader ),
			'seraph_pds' . '\\' . '_render_editor_box',
			$postType,
			'normal',
			'high'
		);
	}
}

function _render_editor_box( $post )
{
	?>

    <div id="seraph_pds_PostUpdater" class="seraph_pds_source">
		<?php _OnPostEditorBox_ShowExtVerInfo(); ?>
		<?php _ShowOperateOptions( ShowOperateOptionsMode_Post, $post ); ?>
	</div>
	
	<?php
}

// #######################################################################
// Admin Menu

function _OnAdminMenu_Show()
{
	add_options_page( 'Post .DOCX Source', '.DOCX Source', 'manage_options', PLUGIN_SETT_PAGE_ID, 'seraph_pds' . '\\' . '_SettingsPage' );
	_OnAdminMenu_ShowEx();
}

// #######################################################################
// Settings

function _SettingsPage()
{
	Plugin::DisplayAdminFooterRateItContent();

	$rmtCfg = PluginRmtCfg::Get();

	_SettingsPage_Action( $rmtCfg );

	$sett = Plugin::SettGet();

	?>

	<?php echo( Ui::PostBoxes_Begin( 'Post .DOCX Source' ) ); ?>

		<?php echo( Ui::PostBoxes_Body_Begin() ); ?>
			<form method="post" action="">
				<?php echo( Ui::PostBox_Begin( 'General', null, null, false ) ); ?>
					<?php echo( Ui::SettBlock_Begin() ); ?>

						<?php echo( Ui::SettBlock_Item_Begin( 'Content Types' ) ); ?>
							<table border="0" cellpadding="0" cellspacing="0"><tbody>
								<tr>
									<?php
												
									$i = 0;
									foreach( GetCompatiblePostsTypes() as $postType )
									{
										if( $i && !( $i % 3 ) )
											echo( '</tr><tr>' );

										$fldId = 'docTypes/' . $postType . '/enable';

										echo( '<td>' );
										echo( Ui::CheckBox( get_post_type_object( $postType ) -> labels -> name, 'seraph_pds' . '/' . $fldId, Gen::GetArrField( $sett, $fldId, true, '/' ), true ) );
										echo( '</td>' );

										$i++;
									}

									?>
								</tr>
							</tbody></table>
							<p class="description">Choose which content types should be processed.</p>
						<?php echo( Ui::SettBlock_Item_End() ); ?>

					<?php echo( Ui::SettBlock_End() ); ?>
							
				<?php echo( Ui::PostBox_End() ); ?>
					
				<?php _OnSettings_ShowBodyBlocks(); ?>
					
				<?php echo( Ui::PostBox_BottomGroupPanel_Begin() ); ?>
					<?php echo( Ui::Sett_SaveBtn( 'seraph_pds_savesettings' ) ); ?>
				<?php echo( Ui::PostBox_BottomGroupPanel_End() ); ?>
			</form>
		<?php echo( Ui::PostBoxes_Body_End() ); ?>
			
		<?php echo( Ui::PostBoxes_Container_Begin() ); ?>
			<?php
					
			_OnSettings_ShowExtVerInfo();

			if( Plugin::RateIt_ShouldShow() !== false )
			{
				echo( Ui::PostBox_Begin( 'Rate It Please', null, null, false ) );
					echo( Plugin::GetRateItContent( Plugin::RateItContent_Box ) );
				echo( Ui::PostBox_End() );
			}

			echo( Ui::PostBox_Begin( 'About', null, null, false ) );
				echo( Plugin::GetAboutPluginContent() );
			echo( Ui::PostBox_End() );

			echo( Ui::PostBox_Begin( 'About Vendor', null, null, false ) );
				echo( Plugin::GetAboutVendorContent() );
			echo( Ui::PostBox_End() );
					
			?>
		<?php echo( Ui::PostBoxes_Container_End() ); ?>

		<div id="postbox-container-2" class="postbox-container">
			<div id="normal-sortables" class="meta-box-sortables ui-sortable">
				<?php _OnSettings_ShowContainer2Blocks(); ?>
			</div>
		</div>

	<?php echo( Ui::PostBoxes_End() ); ?>

	<?php
}

function _SettingsPage_Action( $rmtCfg )
{
	if( isset( $_REQUEST[ 'seraph_pds_savesettings' ] ) )
	{
		$sett = Plugin::SettGet();

		foreach( GetCompatiblePostsTypes() as $postType )
		{
			$fldId = 'docTypes/' . $postType . '/enable';
			Gen::SetArrField( $sett, $fldId, isset( $_REQUEST[ 'seraph_pds' . '/' . $fldId ] ), '/' );

			_OnSettingsPage_Action_SaveSettings_Post( $sett, $postType );
		}

		$res = Plugin::SettSet( $sett );
		if( $res )
			echo( Ui::BannerMsg( Ui::MsgSucc, 'Settings was saved successfully.', Ui::MsgOptDismissible ) );
		else
			echo( Ui::BannerMsg( Ui::MsgErr, 'Settings was not saved.', Ui::MsgOptDismissible ) );
	}
}

// #######################################################################

function _admin_style()
{
	wp_enqueue_style( 'seraph_pds_style', plugins_url( 'style.css', __FILE__ ), array(), '1.5.1' );
}

function _load_javascript()
{
	_load_script( 'editor' );
}

function _load_script( $name )
{
	$url = plugins_url( $name . '.js', __FILE__ );
	echo( '<script src="'. $url . '?v=1.5.1"></script>' );
}

// #######################################################################
// #######################################################################
